unit uERP_Forms;

interface

Uses
  Generics.Collections,
  Windows, SysUtils, Forms, Comctrls, Classes, Uni, DB, dbClient, Controls;

Type
  TRotinasForms = Class

    {$REGION '<< Private Methods >>'}

      private class function BuscaForm(Const ClassName: String; Const Tag: Integer = -1): TForm;
      private class function FormExiste(Const ClassName: String; Const Ativa: Boolean = True; Const Tag: Integer = -1): Boolean;
      private class function AlturaBarraWindows: Integer;
      private class function AbreFormModal(Const Form: TForm; Const Fixo: Boolean = True): TModalResult;
      private class function AbreForm(Const Form: TForm; Const Fixo: Boolean = False; Const PorCima: Boolean = False; Const StyleDefault: Boolean = True): TModalResult;

      private class procedure CentralizaFormChild(Form: TForm; Altura: Boolean = False);
      private class procedure AbreChildForm(Const Form: TForm; Const Fixo: Boolean = False; Const Ativa: Boolean = False; Const Tag: Int64 = -1);
      private class procedure CriaForm(InstanceClass: TComponentClass; Var Reference; Const AbreExistente: Boolean = False; Const UsaKeyPreview: Boolean = False; Const aTag: Int64 = -1);
      private class procedure ConfiguraForm(Const Form: TForm; Const Fixo: Boolean = True; Const PorCima: Boolean = False);

    {$ENDREGION}

    {$REGION '<< Public Methods >>'}

      public

        class function FechaForms(Const FechaEmEdicao: Boolean = True): Boolean;

        class procedure Arredondar(const a_Form: TForm);

        class procedure Login();
        class procedure Usuario_Manutencao();

        class procedure Contador_Manutencao();

        class function Municipio_Manutencao(const a_Modal: Boolean): Int64;

    {$ENDREGION}

  end;

  {$REGION '<< Externals Methots >>'}

    function FormEmEdicao(Const Form: TForm): Boolean;

  {$ENDREGION}

implementation

uses uERP_FrmAutenticacao,
  uERP_BrowserUsuario, uERP_BrowserContador, uERP_BrowserMunicipio;

{$REGION '<< TRotinasForms >>'}

class function TRotinasForms.BuscaForm(Const ClassName: String; Const Tag: Integer = -1): TForm;
var
  I: Integer;
begin
  Result := nil;

  for I := 0 to Application.MainForm.MDIChildCount - 1 do
    begin
      if (Application.MainForm.MDIChildren[I].ClassName= ClassName) and
         ((Tag < 0) or (Tag = Application.MainForm.MDIChildren[I].Tag))
        then begin
          Result := Application.MainForm.MDIChildren[I];
          Break;
        end;
    end;
end;

class function TRotinasForms.FechaForms(const FechaEmEdicao: Boolean): Boolean;
Var
  I: Integer;
Begin
  Result := True;
  With Application.MainForm Do
    For I := MDIChildCount - 1 DownTo 0 Do
      Begin
        If Not FormEmEdicao(MDIChildren[I]) Or FechaEmEdicao Then
          Try
            MDIChildren[I].Close;
            Application.ProcessMessages
          Except
            Result := False
          End
      End;
end;

class function TRotinasForms.FormExiste(Const ClassName: String; Const Ativa: Boolean = True; Const Tag: Integer = -1): Boolean;
var
  Form: TForm;
begin
  Form := BuscaForm(ClassName, Tag);
  Result := Assigned(Form);

  if Assigned(Form) And Ativa then
    begin
      Form.WindowState := wsNormal;
      Form.BringToFront;
      Form.Show;
    end;
end;

class procedure TRotinasForms.CriaForm(InstanceClass: TComponentClass; Var Reference; Const AbreExistente: Boolean = False; Const UsaKeyPreview: Boolean = False; Const aTag: Int64 = -1);
var
  CursorAnt: TCursor;
begin
  TForm(Reference) := Nil;

  CursorAnt := Screen.Cursor;
  try
    Screen.Cursor := crHourGlass;

    if AbreExistente then
      TForm(Reference) := BuscaForm(InstanceClass.ClassName, aTag);

    if not Assigned(TForm(Reference))
      then Application.CreateForm(InstanceClass, Reference);

    Screen.Cursor := CursorAnt
  except
    Screen.Cursor := CursorAnt;
    raise
  end;
end;

class procedure TRotinasForms.ConfiguraForm(Const Form: TForm; Const Fixo: Boolean = True; Const PorCima: Boolean = False);
var
  Icones: TBorderIcons;
begin
  Form.KeyPreview := True;

  Icones := Form.BorderIcons;

  Exclude(Icones, biMinimize);

  { if Fixo then
    Form.BorderStyle := bsDialog
    else
    Form.BorderStyle := bsSizeable; }

  Form.BorderIcons := Icones;

  if PorCima then
    Form.FormStyle := fsStayOnTop
  else
    Form.FormStyle := fsNormal;

  Form.Position := poMainFormCenter;
end;

class procedure TRotinasForms.Contador_Manutencao;
var
  t_BrowserContador: TBrowserContador;
  t_Exists: Boolean;
begin
  t_BrowserContador := nil;
  try
    t_Exists := Assigned(BuscaForm(TBrowserContador.ClassName, -1));

    TRotinasForms.CriaForm(TBrowserContador, t_BrowserContador, True);

    if not t_Exists
      then t_BrowserContador.Initialize();
  finally
    if Assigned(t_BrowserContador)
      then TRotinasForms.AbreChildForm(t_BrowserContador, True, True);
  end;
end;

class function TRotinasForms.AbreFormModal(Const Form: TForm; Const Fixo: Boolean = True): TModalResult;
var
  Icones: TBorderIcons;
begin
  ConfiguraForm(Form, Fixo);
  Result := Form.ShowModal
end;

class function TRotinasForms.AbreForm(Const Form: TForm; Const Fixo: Boolean = False; Const PorCima: Boolean = False; Const StyleDefault: Boolean = True): TModalResult;
begin
  Result := mrNone;
  if StyleDefault then
    ConfiguraForm(Form, Fixo, PorCima);

  Form.Show();
end;

class procedure TRotinasForms.AbreChildForm(Const Form: TForm; Const Fixo: Boolean = False; Const Ativa: Boolean = False; Const Tag: Int64 = -1);
var
  Icones: TBorderIcons;
begin
  if Ativa And FormExiste(Form.ClassName, Ativa, Tag)
    then Exit;

  Form.KeyPreview := True;
  Icones := Form.BorderIcons;

  Include(Icones, biMinimize);

  if Fixo then
    Form.BorderStyle := bsSingle
  else
    Form.BorderStyle := bsSizeable;

  Form.BorderIcons := Icones;

  Form.FormStyle := fsMDIChild;

  Form.Show;
end;

class function TRotinasForms.AlturaBarraWindows: Integer;
Var
  WinBar: THandle;
  WinBarRect: TRect;

Begin
  WinBar := FindWindow('Shell_TrayWnd', '');
  GetWindowRect(WinBar, WinBarRect);

  If ((WinBarRect.Right - WinBarRect.Left) >= Screen.Width) Then
    Result := WinBarRect.Bottom - WinBarRect.Top
  Else
    Result := 0
End;

class procedure TRotinasForms.Arredondar(const a_Form: TForm);
var
  t_Region: HRGN;
begin
  t_Region := CreateRoundRectRgn(0, 0, a_Form.Width, a_Form.Height, 20, 20);
  SetWindowRGN(a_Form.Handle, t_Region, True);
end;

class procedure TRotinasForms.CentralizaFormChild(Form: TForm; Altura: Boolean);
var
  I, Lusada, Husada, AlturaCoolBar, topo, esquerda: Integer;

  Function ValorPositivo(Valor: Integer): Integer;
  Begin
    If Valor < 0 then
      Result := 0
    Else
      Result := Valor;
  End;

Begin
  AlturaCoolBar := 0;

  For I := 0 to (Application.MainForm.ComponentCount - 1) Do
    If Application.MainForm.Components[I] Is TCoolBar Then
      AlturaCoolBar := AlturaCoolBar + (Application.MainForm.Components[I] As TCoolBar).Height;

  Lusada := Form.Width;
  Husada := (AlturaCoolBar + GetSystemMetrics(SM_CYMENUSIZE) + GetSystemMetrics(SM_CYCAPTION));

  If Altura Then
    Form.Height := Screen.Height - (Husada) - AlturaBarraWindows - (GetSystemMetrics(SM_CXBORDER) * 2) - (GetSystemMetrics(SM_CYSIZEFRAME) * 2);

  esquerda := (Application.MainForm.ClientWidth - Lusada) div 2;
  topo := ValorPositivo(Application.MainForm.ClientHeight - Form.Height - Husada) div 2;

  Form.Left := ValorPositivo(esquerda);

  If (Form.FormStyle = fsMDIChild) Then
    Form.Top := topo
  Else
    Form.Top := (topo + Husada)
End;

Function FormEmEdicao(Const Form: TForm): Boolean;
Var
  I, J: Integer;
Begin
  Result := False;
  If Not Assigned(Form) Then
    Exit;
  For I := 0 to Form.ComponentCount - 1 Do
    If Form.Components[I] Is TDataModule Then
      For J := 0 to TDataModule(Form.Components[I]).ComponentCount - 1 Do
        If (Form.Components[I].Components[J] Is TDataSet) and ((TDataSet(Form.Components[I].Components[J]).State in [dsEdit, dsInsert]) And (TDataSet(Form.Components[I].Components[J]).Modified)) Then
          Begin
            Result := True;
            Exit
          End
End;

procedure CancelaDataSetsEmEdicao(const aDataModulo: TDataModule);
var
  I: Integer;
begin
  if not Assigned(aDataModulo) then
    Exit;

  for I := 0 to aDataModulo.ComponentCount - 1 do
    if (aDataModulo.Components[I] is TDataSet) and ((TDataSet(aDataModulo.Components[I]).State in [dsEdit, dsInsert]) And (TDataSet(aDataModulo.Components[I]).Modified)) then
      TDataSet(aDataModulo.Components[I]).Cancel
end;

procedure CancelaDataSetsEmEdicaoDoForm(const aForm: TForm);
var
  I: Integer;
begin
  if not Assigned(aForm) then
    Exit;
  for I := 0 to aForm.ComponentCount - 1 do
    if (aForm.Components[I] is TDataModule) then
      CancelaDataSetsEmEdicao(TDataModule(aForm.Components[I]))
end;

Function FechaForms(Const FechaEmEdicao: Boolean = True; Const FechaPainelNasajon: Boolean = False): Boolean;
Var
  I: Integer;
Begin
  Result := True;
  With Application.MainForm Do
    For I := MDIChildCount - 1 DownTo 0 Do
      Begin
        If Not FormEmEdicao(MDIChildren[I]) Or FechaEmEdicao Then
          Try
            CancelaDataSetsEmEdicaoDoForm(MDIChildren[I]);
            MDIChildren[I].Close;
            Application.ProcessMessages
          Except
            Result := False
          End
      End;
End;

function BuscaForm(Const ClassName: String; Const Tag: Integer = -1): TForm;
begin
  Result := TRotinasForms.BuscaForm(ClassName, Tag)
end;

class procedure TRotinasForms.Login;
var
  FrmAutenticacao: TFrmAutenticacao;
begin
  FrmAutenticacao := nil;
  try
    TRotinasForms.CriaForm(TFrmAutenticacao, FrmAutenticacao);
    TRotinasForms.AbreFormModal(FrmAutenticacao)
  finally
    if Assigned(FrmAutenticacao)
      then FreeAndNil(FrmAutenticacao);
  end;
end;

class function TRotinasForms.Municipio_Manutencao(const a_Modal: Boolean): Int64;
var
  t_BrowserCidade: TBrowserCidade;
  t_Exists: Boolean;
begin
  Result := 0;
  t_BrowserCidade := nil;
  try
    t_Exists := Assigned(BuscaForm(TBrowserCidade.ClassName, -1));

    TRotinasForms.CriaForm(TBrowserCidade, t_BrowserCidade, True);
    if not t_Exists
      then t_BrowserCidade.Initialize(a_Modal);
  finally
    if Assigned(t_BrowserCidade) then
      begin
        if a_Modal then
          begin
            TRotinasForms.AbreFormModal(t_BrowserCidade);
            Result := t_BrowserCidade.GetSelected();
          end
        else
          TRotinasForms.AbreChildForm(t_BrowserCidade, True, True);
      end;
  end;
end;

class procedure TRotinasForms.Usuario_Manutencao;
var
  t_BrowserUsuario: TBrowserUsuario;
  t_Exists: Boolean;
begin
  t_BrowserUsuario := nil;
  try
    t_Exists := Assigned(BuscaForm(TBrowserUsuario.ClassName, -1));

    TRotinasForms.CriaForm(TBrowserUsuario, t_BrowserUsuario, True);

    if not t_Exists
      then t_BrowserUsuario.Initialize();
  finally
    if Assigned(t_BrowserUsuario)
      then TRotinasForms.AbreChildForm(t_BrowserUsuario, True, True);
  end;
end;

{$ENDREGION}

end.
